<?php

//UMA new
namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class ContactInfo extends Model
{
    protected $fillable = [
        'first_name',
        'last_name',
        'first_name_kana',
        'last_name_kana',
        'bod',
        'zip',
        'pref',
        'city',
        'town',
        'building',
        'email',
        'mobile',
        'tel',
        'first_contact_date',
        'second_contact_date',
        'message',
        'privacy_term_accepted',
        'customer_history_id',
        'region',
        'real_estate_info',
        'temporary_member_id',
    ];

    protected $casts = [
        'first_contact_date' => 'array',
        'second_contact_date' => 'array',
    ];

    protected $attributes = [
        'privacy_term_accepted' => 0,
    ];

    protected $hidden = [
        'created_at',
        'updated_at',
    ];
    
    /**
     * setPrivacyTermAcceptedAttribute
     *
     * @param  mixed $v
     * @return void
     */
    public function setPrivacyTermAcceptedAttribute($v)
    {
        $valueOptions = ['1', 'yes', 'on', 'true'];
        $this->attributes['privacy_term_accepted'] = intval(in_array($v, $valueOptions));
    }
        
    /**
     * setMobileAttribute
     *
     * @param  mixed $v
     * @return void
     */
    public function setMobileAttribute($v)
    {
        $this->attributes['mobile'] = str_replace('-', '', $v);
    }
    
    /**
     * setTelAttribute
     *
     * @param  mixed $v
     * @return void
     */
    public function setTelAttribute($v)
    {
        $this->attributes['tel'] = str_replace('-', '', $v);
    }

    
    /**
     * 一个咨询属于一个用户
     * コンサルトは顧客のものです
     * @return belongsTo
     */
    public function customer_history()
    {
        return $this->belongsTo(CustomerHistory::class, 'customer_history_id');
    }
    
    /**
     * 获取地址
     * 住所を取得する
     * @return string
     */
    public function getFullAddressAttribute()
    {
        return $this->city.$this->town.$this->building;
    }
    
    /**
     * setFirstContactDateAttribute
     *
     * @param  mixed $v
     * @return void
     */
    public function setFirstContactDateAttribute($v)
    {
        $date = $this->castAttributeAsJson('first_contact_date', $v);
        $date = json_decode($date, true);
        $date['date'] = Carbon::parse($date['date'])->format('Y-m-d');
        $this->attributes['first_contact_date'] = json_encode($date);
    }
    
    /**
     * setSecondContactDateAttribute
     *
     * @param  mixed $v
     * @return void
     */
    public function setSecondContactDateAttribute($v)
    {
        $date = $this->castAttributeAsJson('second_contact_date', $v);
        $date = json_decode($date, true);
        $date['date'] = Carbon::parse($date['date'])->format('Y-m-d');
        $this->attributes['second_contact_date'] = json_encode($date);
    }

    /**
     * 获取 kingtone 需要的数据
     * kintoneに必要なデータを取得する
     * @return array
     */
    public function getKintoneData()
    {
        $exclude = [
            'first_name_kana',
            'last_name_kana',
            'updated_at',
        ];
        $contactData = array_diff_key($this->toArray(), array_flip($exclude));
        $contactData['first_contact_date'] = implode(' ', $this->first_contact_date);
        $contactData['second_contact_date'] = implode(' ', $this->second_contact_date);
        $contactData['contact_infos_id'] = $this->id;
        $contactData = array_map(function ($item) {
            return ['value' => $item];
        }, $contactData);
        $inputData = $this->customer_history->getKintoneData();
        return array_merge($inputData, $contactData);
    }

    public function isRepeatSubmit() {
        return now()->diffInMinutes($this->created_at) < 1;
    }

    public function scopeSearch($query, $datas) {
        $fields = ['first_name', 'last_name', 'mobile', 'tel', 'first_contact_date'];
        if(!empty($datas)) {
            foreach($datas as $k => $v) {
                if(strlen($v)) {
                    if(in_array($k, $fields)) {
                        if($k != 'first_contact_date') {
                            $query->where($k ,$v);
                        }
                        else {
                            $query->where('first_contact_date->date', $v);
                        }
                    }
                }
            }
        }
        
        return $query;

    }
}
